#include "quote_spi.h"
#include <iostream>
#include <stdio.h>
#include <string>

using namespace std;

void MyQuoteSpi::OnError(XTPRI *error_info, bool is_last)
{
	cout << "--->>> "
		 << "OnRspError" << endl;
	IsErrorRspInfo(error_info);
}

MyQuoteSpi::MyQuoteSpi()
{
}

MyQuoteSpi::~MyQuoteSpi()
{
}
void MyQuoteSpi::setDepthMarketData(QJsonObject *pRealPriceJsonObject)
{
	this->pRealPriceJsonObject = pRealPriceJsonObject;
}

void MyQuoteSpi::OnDisconnected(int reason)
{
	cout << "--->>> "
		 << "OnDisconnected quote" << endl;
	cout << "--->>> Reason = " << reason << endl;
	//断线后，可以重新连接
	//重新连接成功后，需要重新向服务器发起订阅请求
}

void MyQuoteSpi::OnSubMarketData(XTPST *ticker, XTPRI *error_info, bool is_last)
{
	cout << "OnRspSubMarketData -----" << endl;
}

void MyQuoteSpi::OnUnSubMarketData(XTPST *ticker, XTPRI *error_info, bool is_last)
{
	cout << "OnRspUnSubMarketData -----------" << endl;
}

void MyQuoteSpi::OnDepthMarketData(XTPMD *market_data, int64_t bid1_qty[], int32_t bid1_count, int32_t max_bid1_count, int64_t ask1_qty[], int32_t ask1_count, int32_t max_ask1_count)
{
	if (pRealPriceJsonObject == NULL)
	{
		cout << "pRealPriceJsonObject == NULL error" << endl;
		return;
	}
	// cout << "OnDepthMarketData -----------" << endl;
	// cout << "last_price: " << market_data->last_price << endl;
	// QJsonObject dataObj;
	// QJsonArray dataArr;
	if (!(*pRealPriceJsonObject)["list"].isArray())
	{
		cout << "pRealPriceJsonObject isArray NULL" << endl;
		(*pRealPriceJsonObject)["list"] = QJsonArray();
		// return;
	}
	QJsonArray array = (*pRealPriceJsonObject)["list"].toArray();

	if (array.size() > 50)
	{
		array.pop_front();
	}
	QJsonObject dataObj;
	dataObj["nowPrice"] = market_data->last_price;
	dataObj["yest_close"] = market_data->pre_close_price;
	dataObj["avgPrice"] = market_data->avg_price;
	dataObj["lower_price"] = market_data->low_price;
	dataObj["max_price"] = market_data->high_price;
	dataObj["close_price"] = market_data->close_price;
	dataObj["open_price"] = market_data->open_price;
	dataObj["trade_num"] = market_data->trades_count;
	array.push_back(dataObj);
	pRealPriceJsonObject->insert("list", array);
	// pRealPriceJsonObject->insert("dataList", dataArr);
	pRealPriceJsonObject->insert("count", array.size());
	pRealPriceJsonObject->insert("name", "白云机场");
	pRealPriceJsonObject->insert("market", "沪深");
	pRealPriceJsonObject->insert("code", "600002");
}

void MyQuoteSpi::OnSubOrderBook(XTPST *ticker, XTPRI *error_info, bool is_last)
{
}

void MyQuoteSpi::OnUnSubOrderBook(XTPST *ticker, XTPRI *error_info, bool is_last)
{
}

void MyQuoteSpi::OnSubTickByTick(XTPST *ticker, XTPRI *error_info, bool is_last)
{
}

void MyQuoteSpi::OnUnSubTickByTick(XTPST *ticker, XTPRI *error_info, bool is_last)
{
}

void MyQuoteSpi::OnOrderBook(XTPOB *order_book)
{
}

void MyQuoteSpi::OnTickByTick(XTPTBT *tbt_data)
{
}

void MyQuoteSpi::OnQueryAllTickers(XTPQSI *ticker_info, XTPRI *error_info, bool is_last)
{
	cout << "OnQueryAllTickers -----------" << endl;
	cout << std::string(ticker_info->ticker_name) << endl;
	cout << ticker_info->pre_close_price << endl;
}

void MyQuoteSpi::OnQueryTickersPriceInfo(XTPTPI *ticker_info, XTPRI *error_info, bool is_last)
{
}

void MyQuoteSpi::OnSubscribeAllMarketData(XTP_EXCHANGE_TYPE exchange_id, XTPRI *error_info)
{
}

void MyQuoteSpi::OnUnSubscribeAllMarketData(XTP_EXCHANGE_TYPE exchange_id, XTPRI *error_info)
{
}

void MyQuoteSpi::OnSubscribeAllOrderBook(XTP_EXCHANGE_TYPE exchange_id, XTPRI *error_info)
{
}

void MyQuoteSpi::OnUnSubscribeAllOrderBook(XTP_EXCHANGE_TYPE exchange_id, XTPRI *error_info)
{
}

void MyQuoteSpi::OnSubscribeAllTickByTick(XTP_EXCHANGE_TYPE exchange_id, XTPRI *error_info)
{
}

void MyQuoteSpi::OnUnSubscribeAllTickByTick(XTP_EXCHANGE_TYPE exchange_id, XTPRI *error_info)
{
}

void MyQuoteSpi::OnSubscribeAllOptionMarketData(XTP_EXCHANGE_TYPE exchange_id, XTPRI *error_info)
{
}

void MyQuoteSpi::OnUnSubscribeAllOptionMarketData(XTP_EXCHANGE_TYPE exchange_id, XTPRI *error_info)
{
}

void MyQuoteSpi::OnSubscribeAllOptionOrderBook(XTP_EXCHANGE_TYPE exchange_id, XTPRI *error_info)
{
}

void MyQuoteSpi::OnUnSubscribeAllOptionOrderBook(XTP_EXCHANGE_TYPE exchange_id, XTPRI *error_info)
{
}

void MyQuoteSpi::OnSubscribeAllOptionTickByTick(XTP_EXCHANGE_TYPE exchange_id, XTPRI *error_info)
{
}

void MyQuoteSpi::OnUnSubscribeAllOptionTickByTick(XTP_EXCHANGE_TYPE exchange_id, XTPRI *error_info)
{
}

bool MyQuoteSpi::IsErrorRspInfo(XTPRI *pRspInfo)
{
	// 如果ErrorID != 0, 说明收到了错误的响应
	bool bResult = ((pRspInfo) && (pRspInfo->error_id != 0));
	if (bResult)
		cout << "--->>> ErrorID=" << pRspInfo->error_id << ", ErrorMsg=" << pRspInfo->error_msg << endl;
	return bResult;
}
